home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gscsepr.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  13.0 KB  |  414 lines

  1. /* Copyright (C) 1994, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gscsepr.c,v 1.6 2000/09/19 19:00:27 lpd Exp $ */
  20. /* Separation color space and operation definition */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsfunc.h"
  25. #include "gsrefct.h"
  26. #include "gsmatrix.h"        /* for gscolor2.h */
  27. #include "gscsepr.h"
  28. #include "gxcspace.h"
  29. #include "gxfixed.h"        /* for gxcolor2.h */
  30. #include "gxcolor2.h"        /* for gs_indexed_map */
  31. #include "gzstate.h"        /* for pgs->overprint */
  32.  
  33. /* ---------------- Color space ---------------- */
  34.  
  35. gs_private_st_composite(st_color_space_Separation, gs_paint_color_space,
  36.             "gs_color_space_Separation",
  37.             cs_Separation_enum_ptrs, cs_Separation_reloc_ptrs);
  38.  
  39. /* Define the Separation color space type. */
  40. private cs_proc_base_space(gx_alt_space_Separation);
  41. private cs_proc_equal(gx_equal_Separation);
  42. private cs_proc_init_color(gx_init_Separation);
  43. private cs_proc_concrete_space(gx_concrete_space_Separation);
  44. private cs_proc_concretize_color(gx_concretize_Separation);
  45. private cs_proc_remap_concrete_color(gx_remap_concrete_Separation);
  46. private cs_proc_install_cspace(gx_install_Separation);
  47. private cs_proc_adjust_cspace_count(gx_adjust_cspace_Separation);
  48. const gs_color_space_type gs_color_space_type_Separation = {
  49.     gs_color_space_index_Separation, true, false,
  50.     &st_color_space_Separation, gx_num_components_1,
  51.     gx_alt_space_Separation, gx_equal_Separation,
  52.     gx_init_Separation, gx_restrict01_paint_1,
  53.     gx_concrete_space_Separation,
  54.     gx_concretize_Separation, gx_remap_concrete_Separation,
  55.     gx_default_remap_color, gx_install_Separation,
  56.     gx_adjust_cspace_Separation, gx_no_adjust_color_count
  57. };
  58.  
  59. /* GC procedures */
  60.  
  61. private 
  62. ENUM_PTRS_WITH(cs_Separation_enum_ptrs, gs_color_space *pcs)
  63. {
  64.     return ENUM_USING(*pcs->params.separation.alt_space.type->stype,
  65.               &pcs->params.separation.alt_space,
  66.               sizeof(pcs->params.separation.alt_space), index - 1);
  67. }
  68. ENUM_PTR(0, gs_color_space, params.separation.map);
  69. ENUM_PTRS_END
  70. private RELOC_PTRS_WITH(cs_Separation_reloc_ptrs, gs_color_space *pcs)
  71. {
  72.     RELOC_PTR(gs_color_space, params.separation.map);
  73.     RELOC_USING(*pcs->params.separation.alt_space.type->stype,
  74.         &pcs->params.separation.alt_space,
  75.         sizeof(gs_base_color_space));
  76. }
  77. RELOC_PTRS_END
  78.  
  79. /* Get the alternate space for a Separation space. */
  80. private const gs_color_space *
  81. gx_alt_space_Separation(const gs_color_space * pcs)
  82. {
  83.     return (const gs_color_space *)&(pcs->params.separation.alt_space);
  84. }
  85.  
  86. /* Test whether one Separation color space equals another. */
  87. private bool
  88. gx_equal_Separation(const gs_color_space *pcs1, const gs_color_space *pcs2)
  89. {
  90.     return (gs_color_space_equal(gx_alt_space_Separation(pcs1),
  91.                  gx_alt_space_Separation(pcs2)) &&
  92.         pcs1->params.separation.sname == pcs2->params.separation.sname &&
  93.         ((pcs1->params.separation.map->proc.tint_transform ==
  94.             pcs2->params.separation.map->proc.tint_transform &&
  95.           pcs1->params.separation.map->proc_data ==
  96.             pcs2->params.separation.map->proc_data) ||
  97.          !memcmp(pcs1->params.separation.map->values,
  98.              pcs2->params.separation.map->values,
  99.              pcs1->params.separation.map->num_values *
  100.              sizeof(pcs1->params.separation.map->values[0]))));
  101. }
  102.  
  103. /* Get the concrete space for a Separation space. */
  104. /* (We don't support concrete Separation spaces yet.) */
  105. private const gs_color_space *
  106. gx_concrete_space_Separation(const gs_color_space * pcs,
  107.                  const gs_imager_state * pis)
  108. {
  109.     const gs_color_space *pacs =
  110.     (const gs_color_space *)&pcs->params.separation.alt_space;
  111.  
  112.     return cs_concrete_space(pacs, pis);
  113. }
  114.  
  115. /* Install a Separation color space. */
  116. private int
  117. gx_install_Separation(const gs_color_space * pcs, gs_state * pgs)
  118. {
  119.     return (*pcs->params.separation.alt_space.type->install_cspace)
  120.     ((const gs_color_space *) & pcs->params.separation.alt_space, pgs);
  121. }
  122.  
  123. /* Adjust the reference count of a Separation color space. */
  124. private void
  125. gx_adjust_cspace_Separation(const gs_color_space * pcs, int delta)
  126. {
  127.     rc_adjust_const(pcs->params.separation.map, delta,
  128.             "gx_adjust_Separation");
  129.     (*pcs->params.separation.alt_space.type->adjust_cspace_count)
  130.     ((const gs_color_space *)&pcs->params.separation.alt_space, delta);
  131. }
  132.  
  133. /* ------ Constructors/accessors ------ */
  134.  
  135. /*
  136.  * The default separation tint transformation function. This will just return
  137.  * the information in the cache or, if the cache is of zero size, set all
  138.  * components in the alternative color space to 0.
  139.  *
  140.  * No special cases are provided for this routine, as the use of separations
  141.  * (particular in this form) is sufficiently rare to not have a significant
  142.  * performance impact.
  143.  */
  144. private int
  145. map_tint_value(const gs_separation_params * pcssepr, floatp in_val,
  146.            float *out_vals)
  147. {
  148.     int ncomps =
  149.     cs_num_components((const gs_color_space *)&pcssepr->alt_space);
  150.     int nentries = pcssepr->map->num_values / ncomps;
  151.     int indx;
  152.     const float *pv = pcssepr->map->values;
  153.     int i;
  154.  
  155.     if (nentries == 0) {
  156.     for (i = 0; i < ncomps; i++)
  157.         out_vals[i] = 0.0;
  158.     return 0;
  159.     }
  160.     if (in_val > 1)
  161.     indx = nentries - 1;
  162.     else if (in_val <= 0)
  163.     indx = 0;
  164.     else
  165.     indx = (int)(in_val * nentries + 0.5);
  166.     pv += indx * ncomps;
  167.  
  168.     for (i = 0; i < ncomps; i++)
  169.     out_vals[i] = pv[i];
  170.     return 0;
  171. }
  172.  
  173. /*
  174.  *  Allocate the indexed map required by a separation color space. 
  175.  */
  176. private gs_indexed_map *
  177. alloc_separation_map(const gs_color_space * palt_cspace, int cache_size,
  178.              gs_memory_t * pmem)
  179. {
  180.     int num_values =
  181.     (cache_size == 0 ? 0 :
  182.      cache_size * gs_color_space_num_components(palt_cspace));
  183.     gs_indexed_map *pimap;
  184.     int code = alloc_indexed_map(&pimap, num_values, pmem,
  185.                  "gs_cspace_build_Separation");
  186.  
  187.     if (code < 0)
  188.     return 0;
  189.     pimap->proc.tint_transform = map_tint_value;
  190.     return pimap;
  191. }
  192.  
  193. /*
  194.  * Build a separation color space.
  195.  *
  196.  * The values array provided with separation color spaces is actually cached
  197.  * information, but filled in by the client. The alternative space is the
  198.  * color space in which the tint procedure will provide alternative colors.
  199.  */
  200. int
  201. gs_cspace_build_Separation(
  202.                   gs_color_space ** ppcspace,
  203.                   gs_separation_name sname,
  204.                   const gs_color_space * palt_cspace,
  205.                   int cache_size,
  206.                   gs_memory_t * pmem
  207. )
  208. {
  209.     gs_color_space *pcspace = 0;
  210.     gs_separation_params *pcssepr = 0;
  211.     int code;
  212.  
  213.     if (palt_cspace == 0 || !palt_cspace->type->can_be_alt_space)
  214.     return_error(gs_error_rangecheck);
  215.  
  216.     code = gs_cspace_alloc(&pcspace, &gs_color_space_type_Separation, pmem);
  217.     if (code < 0)
  218.     return code;
  219.     pcssepr = &pcspace->params.separation;
  220.     pcssepr->map = alloc_separation_map(palt_cspace, cache_size, pmem);
  221.     if (pcssepr->map == 0) {
  222.     gs_free_object(pmem, pcspace, "gs_cspace_build_Separation");
  223.     return_error(gs_error_VMerror);
  224.     }
  225.     pcssepr->sname = sname;
  226.     gs_cspace_init_from((gs_color_space *) & pcssepr->alt_space, palt_cspace);
  227.     *ppcspace = pcspace;
  228.     return 0;
  229. }
  230.  
  231. /*
  232.  * Get the cached value array for a separation color space. This will return
  233.  * a null pointer if the color space is not a separation color space, or if
  234.  * the separation color space has a cache size of 0.
  235.  */
  236. float *
  237. gs_cspace_get_sepr_value_array(const gs_color_space * pcspace)
  238. {
  239.     if (gs_color_space_get_index(pcspace) != gs_color_space_index_Separation)
  240.     return 0;
  241.     return pcspace->params.separation.map->values;
  242. }
  243.  
  244. /*
  245.  * Set the tint transformation procedure used by a Separation color space.
  246.  */
  247. int
  248. gs_cspace_set_sepr_proc(gs_color_space * pcspace,
  249.         int (*proc) (P3(const gs_separation_params *, floatp, float *)))
  250. {
  251.     gs_indexed_map *pimap;
  252.  
  253.     if (gs_color_space_get_index(pcspace) != gs_color_space_index_Separation)
  254.     return_error(gs_error_rangecheck);
  255.     pimap = pcspace->params.separation.map;
  256.     pimap->proc.tint_transform = proc;
  257.     pimap->proc_data = 0;
  258.     return 0;
  259. }
  260.  
  261. /* Map a Separation tint using a Function. */
  262. private int
  263. map_sepr_using_function(const gs_separation_params * pcssepr,
  264.             floatp in_val, float *out_vals)
  265. {
  266.     float in = in_val;
  267.     gs_function_t *const pfn = pcssepr->map->proc_data;
  268.  
  269.     return gs_function_evaluate(pfn, &in, out_vals);
  270. }
  271.  
  272. /*
  273.  * Set the Separation tint transformation procedure to a Function.
  274.  */
  275. int
  276. gs_cspace_set_sepr_function(const gs_color_space *pcspace, gs_function_t *pfn)
  277. {
  278.     gs_indexed_map *pimap;
  279.  
  280.     if (gs_color_space_get_index(pcspace) != gs_color_space_index_Separation ||
  281.     pfn->params.m != 1 ||
  282.     pfn->params.n !=
  283.       gs_color_space_num_components((const gs_color_space *)
  284.                     &pcspace->params.separation.alt_space)
  285.     )
  286.     return_error(gs_error_rangecheck);
  287.     pimap = pcspace->params.separation.map;
  288.     pimap->proc.tint_transform = map_sepr_using_function;
  289.     pimap->proc_data = pfn;
  290.     return 0;
  291. }
  292.  
  293. /*
  294.  * If the Separation tint transformation procedure is a Function,
  295.  * return the function object, otherwise return 0.
  296.  */
  297. gs_function_t *
  298. gs_cspace_get_sepr_function(const gs_color_space *pcspace)
  299. {
  300.     if (gs_color_space_get_index(pcspace) == gs_color_space_index_Separation &&
  301.     pcspace->params.separation.map->proc.tint_transform ==
  302.       map_sepr_using_function)
  303.     return pcspace->params.separation.map->proc_data;
  304.     return 0;
  305. }
  306.  
  307. /* ---------------- Graphics state ---------------- */
  308.  
  309. /* setoverprint */
  310. void
  311. gs_setoverprint(gs_state * pgs, bool ovp)
  312. {
  313.     pgs->overprint = ovp;
  314. }
  315.  
  316. /* currentoverprint */
  317. bool
  318. gs_currentoverprint(const gs_state * pgs)
  319. {
  320.     return pgs->overprint;
  321. }
  322.  
  323. /* setoverprintmode */
  324. int
  325. gs_setoverprintmode(gs_state * pgs, int mode)
  326. {
  327.     if (mode < 0 || mode > 1)
  328.     return_error(gs_error_rangecheck);
  329.     pgs->overprint_mode = mode;
  330.     return 0;
  331. }
  332.  
  333. /* currentoverprintmode */
  334. int
  335. gs_currentoverprintmode(const gs_state * pgs)
  336. {
  337.     return pgs->overprint_mode;
  338. }
  339.  
  340. /* ------ Internal procedures ------ */
  341.  
  342. /* Initialize a Separation color. */
  343.  
  344. private void
  345. gx_init_Separation(gs_client_color * pcc, const gs_color_space * pcs)
  346. {
  347.     pcc->paint.values[0] = 1.0;
  348. }
  349.  
  350. /* Remap a Separation color. */
  351.  
  352. private int
  353. gx_concretize_Separation(const gs_client_color *pc, const gs_color_space *pcs,
  354.              frac *pconc, const gs_imager_state *pis)
  355. {
  356.     float tint = pc->paint.values[0];
  357.     int code;
  358.     gs_client_color cc;
  359.     const gs_color_space *pacs =
  360.     (const gs_color_space *)&pcs->params.separation.alt_space;
  361.  
  362.     if (tint < 0)
  363.     tint = 0;
  364.     else if (tint > 1)
  365.     tint = 1;
  366.     /* We always map into the alternate color space. */
  367.     code = (*pcs->params.separation.map->proc.tint_transform) (&pcs->params.separation, tint, &cc.paint.values[0]);
  368.     if (code < 0)
  369.     return code;
  370.     return (*pacs->type->concretize_color) (&cc, pacs, pconc, pis);
  371. }
  372.  
  373. private int
  374. gx_remap_concrete_Separation(const frac * pconc,
  375.     gx_device_color * pdc, const gs_imager_state * pis, gx_device * dev,
  376.                  gs_color_select_t select)
  377. {                /* We don't support concrete Separation colors yet. */
  378.     return_error(gs_error_rangecheck);
  379. }
  380.  
  381. /* ---------------- Notes on real Separation colors ---------------- */
  382.  
  383. typedef ulong gs_separation;    /* BOGUS */
  384.  
  385. #define gs_no_separation ((gs_separation)(-1L))
  386.  
  387. #define dev_proc_lookup_separation(proc)\
  388.   gs_separation proc(P4(gx_device *dev, const byte *sname, uint len,\
  389.     gx_color_value *num_levels))
  390.  
  391. #define dev_proc_map_tint_color(proc)\
  392.   gx_color_index proc(P4(gx_device *dev, gs_separation sepr, bool overprint,\
  393.     gx_color_value tint))
  394.  
  395. /*
  396.  * In principle, setting a Separation color space, or setting the device
  397.  * when the current color space is a Separation space, calls the
  398.  * lookup_separation device procedure to obtain the separation ID and
  399.  * the number of achievable levels.  Currently, the only hooks for doing
  400.  * this are unsuitable: gx_set_cmap_procs isn't called when the color
  401.  * space changes, and doing it in gx_remap_Separation is inefficient.
  402.  * Probably the best approach is to call gx_set_cmap_procs whenever the
  403.  * color space changes.  In fact, if we do this, we can probably short-cut
  404.  * two levels of procedure call in color remapping (gx_remap_color, by
  405.  * turning it into a macro, and gx_remap_DeviceXXX, by calling the
  406.  * cmap_proc procedure directly).  Some care will be required for the
  407.  * implicit temporary resetting of the color space in [color]image.
  408.  *
  409.  * For actual remapping of Separation colors, we need cmap_separation_direct
  410.  * and cmap_separation_halftoned, just as for the other device color spaces.
  411.  * So we need to break apart gx_render_gray in gxdither.c so it can also
  412.  * do the job for separations.
  413.  */
  414.